package org.qufenqi.api.common;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Getter;

import java.io.Serializable;
@Getter
//为空的值不被json传递
@JsonInclude(JsonInclude.Include.NON_NULL)
public class CommonResponse<T> implements Serializable{
    private int status;//状态码
    private String msg;//消息
    private T data;

    //构造方法
    private CommonResponse(int status){
        this.status = status;
    }
    private CommonResponse(int status,String msg){
        this.status = status;
        this.msg = msg;
    }
    private CommonResponse(int status,String msg,T date){
        this.status = status;
        this.msg = msg;
        this.data = date;
    }
    private CommonResponse(int status,T date){
        this.status = status;
        this.data = date;
    }

    //外部调用构造方法
    @JsonIgnore
    public boolean isSuccess(){
        return this.status == ResponseCode.SUCCESS.getCode();
    }
    //成功
    public static <T>CommonResponse <T> responseForSuccess(){
        return new CommonResponse<T>(ResponseCode.SUCCESS.getCode());
    }
    public static <T>CommonResponse <T> responseForSuccess(T date){
            return new CommonResponse<T>(ResponseCode.SUCCESS.getCode(),date);
        }
        //避免将String的date错误识别为msg
    public static <T>CommonResponse <T> responseForSuccessMessage(String msg){
        return new CommonResponse<T>(ResponseCode.SUCCESS.getCode(),msg);
    }
    public static <T>CommonResponse <T> responseForSuccessMessage(String msg,T date){
        return new CommonResponse<T>(ResponseCode.SUCCESS.getCode(),msg,date);
    }
    //失败
    public static <T>CommonResponse <T> responseForErrorMessage(String msg){
        return new CommonResponse<T>(ResponseCode.ERROR.getCode(),msg);
    }
    //未登录
    public static <T>CommonResponse <T> responseForLogin(){
        return new CommonResponse<T>(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDecription());
    }
    //表单提交错误
    public static <T>CommonResponse <T> responseForIllegal(){
        return new CommonResponse<T>(ResponseCode.ILLEGAL_DATE.getCode(),ResponseCode.ILLEGAL_DATE.getDecription());
    }

}
